<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetNotifyTemplates;

use App\Base\Resources\BaseAdminResource;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigs;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;
use App\Plugins\FcpApi\TweetPluginUsages\TweetPluginUsagesConfig;
use App\Plugins\FcpApi\TweetPluginUsages\TweetPluginUsagesService;

class FsResource extends BaseAdminResource
{
    public function toArray($request)
    {
        // dd(1);
        // form 字段
        $formMap = FsConfig::FORM_FIELDS_MAP;
        $formMapFieldsArr = [];
        foreach ($formMap as $k => $dbField) {
            $formMapFieldsArr[$dbField] = $this->$dbField;
        }
        // 获取默认语言code
        $defaultCode = TweetPluginUsagesService::getDefaultLanguage();
        $lang_code = $request->header('lang-code', $defaultCode);
        foreach (TweetPluginUsagesConfig::MULTILINGUAL_OPTION as $v) {
            // dd($lang_code);
            if ($v['nickname'] == $lang_code) {
                // dd($v);
                $lang_code = $v['key'];
            }
        }

        // 推送渠道
        $push_channel_arr = explode(',', $this->push_channel);
        $push_channelIcon_arr = [];
        foreach (FsConfig::PUSH_CHANNEL as $v) {
            if (in_array($v['value'], $push_channel_arr)) {
                $push_channelIcon_arr[] = $v['icon'];
            }
        }
        // dd($push_channelName_arr);
        // 禁止推送渠道
        $push_disable_arr = explode(',', $this->push_disable);

        // 多语言过滤
        $template_arr = json_decode($this->template, true);
        $languages = TweetConfigs::where('item_key', FsConfig::LANGUAGE_MENUS)->first();
        // $languageArr = TweetConfigService::getLanguageStatus();
        $language = json_decode($languages['item_value'], true);
        // dd($language);
        $langCodeArr = [];
        foreach ($language as $v) {
            $langCodeArr[] = $v['langTag'];
        }
        // dd($langCodeArr);
        // 添加的语言是否在后台设置的语言范围内
        $newName = [];
        foreach ($template_arr as $f) {
            if (in_array($f['lang_code'], $langCodeArr)) {
                $arr = [];
                $arr = $f;
                $newName[] = $f;
            }
        }
        // 获取展示的语言字段
        $template_name = [];
        foreach ($newName as  $v) {
            if ($lang_code == $v['lang_code']) {
                $template_name = $v;
            }
        }
        // dd($newName);
        FsModel::where('id', $this->id)->update(['template' => json_encode($newName)]);
        // 默认字段
        $default = [
            'key'       => $this->id,
            'id'        => $this->id,
            'is_enable' => boolval($this->is_enable),
            'disabled'  => false,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,

            'nickname'  => $this->nickname,
            'more_json'  => $this->more_json,
            'more_json_decode'  => json_decode($this->more_json, true),
            'push_channel_arr' => $push_channel_arr,
            'push_disable_arr' => $push_disable_arr,
            'template_decode' => $newName,
            'variable_desc_decode' => json_decode($this->variable_desc, true),
            'template_name' => $template_name,
            'push_channelIcon_arr' => $push_channelIcon_arr,
        ];

        // 合并
        $arr = array_merge($formMapFieldsArr, $default);

        return $arr;
    }
}
